# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from model.base import BaseModel
from sentence_transformers import SentenceTransformer
from utils import MODELS


@MODELS.register_module("sentence_transformer")
class SentenceTransformerModel(BaseModel):
    MODE = "sentence_transformer"

    def __init__(self, model_name, **kwargs):
        super().__init__(model_name, **kwargs)
        self.model_path = kwargs.get('model_path')
        if not self.model_path:
            raise ValueError(f"SentenceTransformersModel {model_name} must have path_or_dir")
        self.model_kwargs = kwargs.get('model_kwargs', {})
        self.model = SentenceTransformer(self.model_path, **self.model_kwargs)

    def infer(self, row_data):
        row_data = self._preprocess_row_data(row_data)
        assert 'text' in row_data, "Dataset must contain the text column"
        embeddings = self.model.encode(row_data['text'], normalize_embeddings=True, show_progress_bar=False)
        return {
            'embedding': embeddings
        }
